/*
 * Last Modify: 2005��1��20�� 11:16:57
 */
package com.ckzp.plugins.datalist.active;


import com.ckzp.core.util.StringUtil;

import java.sql.Connection;
import java.util.List;

/**
 * Filename: ESelect.java
 * <p/>
 * Copyright:    Copyright (c) 2001-2004 BlueStar,Inc.All Rights Reserved.
 * Company:     huott
 * Author:      <a href="mailto:lanxingsc@163.com">wuyang</a>
 * Telephone:   86-28-85239210
 * Date: 2004-9-6
 * Time: 11:53:05
 * Version: 2.0.1.1104
 */
public class ESelect extends Active implements EActive {


    public static EActive dao() {
        return new ESelect();
    }

    /**
     * @param conn
     */
    public ESelect(Connection conn) {
        this.conn = conn;
        this.tag = false;
        this.name = "select";
    }

    /**
     * @param DB_NAME
     */
    public ESelect(String DB_NAME) {
        //   this.conn = DbUtil.getConnections(DB_NAME);
        this.tag = false;
        this.name = "select";
    }

    /**
     *
     */
    public ESelect() {
        this.name = "select";
    }


    public String showColor(String currentValue) {
        String[] showColor1 = this.showColor;
        for (int k = 0; k < showColor1.length - 1; k += 2) {
            if (showColor1[k].equals(currentValue)) {
                return showColor1[k + 1];
            }
        }
        return "";
    }

    /**
     * *
     *
     * @return
     */
    public String showHtml() {
        if (dataList == null || dataList.size() == 0) {
            doAction();
        }
        int kk = 0;
        boolean havaValue = false;
        StringBuffer html = new StringBuffer();
        if (currentValue == null) currentValue = "";
        html.append("");
        if (readOnly) {
            String[] showColor1 = this.showColor;
            for (; kk < dataList.size(); kk++) {
                String[] s = (String[]) dataList.get(kk);
                if (currentValue.equals(s[0])) {
                    havaValue = true;
                    String reValue = s[1];
                    for (int k = 0; k < showColor1.length - 1; k++) {
                        if (showColor1[k].equals(s[0]))
                            reValue = "<span style=\"color:" + showColor1[k + 1] + "\">" + reValue + "</span>";
                    }
                    html.append(reValue);
                    break;
                }
            }
            if (!havaValue && !canNull) {
                if (StringUtil.getString(defaultStr).length() > 0) {
                    html.append(this.defaultStr);
                } else {
                    html.append(currentValue);
                }
            }
        } else {
            html.append("<select id=\"" + name + "\" name=\"" + name + "\" " + event + " style=\"" + style + "\">\r\n");
            if (canNull)
                html.append("<option value=\"\" class=\"select-option-default\">" + defaultStr + "</option>\r\n");
            if (dataList != null) {
                for (kk = 0; kk < dataList.size(); kk++) {
                    String[] s = (String[]) dataList.get(kk);
                    html.append("<option value=\"" + s[0] + "\"");
                    if (this.currentValue.equals(s[0])) html.append(" selected");
                    html.append(">" + s[1] + "</option>\r\n");
                }
            }
            html.append("</select>\r\n");
        }
        return html.toString();
    }

    /**
     * @return
     */
    public String showNoColorHtml() {
        if (dataList == null || dataList.size() == 0) {
            doAction();
        }
        int kk = 0;
        boolean havaValue = false;
        StringBuffer html = new StringBuffer();
        if (currentValue == null) currentValue = "";
        html.append("");
        if (readOnly) {
            String[] showColor1 = this.showColor;
            for (; kk < dataList.size(); kk++) {
                String[] s = (String[]) dataList.get(kk);
                if (currentValue.equals(s[0])) {
                    havaValue = true;
                    String reValue = s[1];
                    html.append(reValue);
                    break;
                }
            }
            if (!havaValue && !canNull) {
                if (StringUtil.getString(defaultStr).length() > 0) {
                    html.append(this.defaultStr);
                } else {
                    html.append(currentValue);
                }
            }
        } else {
            html.append("<select id=\"" + name + "\" name=\"" + name + "\" " + event + " style=\"" + style + "\">\r\n");
            if (canNull) html.append("<option value=\"\">" + defaultStr + "</option>\r\n");
            if (dataList != null) {
                for (kk = 0; kk < dataList.size(); kk++) {
                    String[] s = (String[]) dataList.get(kk);
                    html.append("<option value=\"" + s[0] + "\"");
                    if (this.currentValue.equals(s[0])) html.append(" selected");
                    html.append(">" + s[1] + "</option>\r\n");
                }
            }
            html.append("</select>\r\n");
        }
        return html.toString();
    }

    public List getDataList() {
        return dataList;
    }

    public EActive clone() throws CloneNotSupportedException {
        return (EActive) super.clone();
    }
}
